<?php 
namespace Home\Controller;
class ContributeController extends HomeController
{
    public function index()
    {
        $category=M('category')->where(array('display'=>'1'))->field('id,cname')->select();


        if ($_SESSION['user'] == '') {
            redirect(U('Login/login'),1,'请您先登录账号!');
        }
        if (!empty($_GET['aid']) ) {
            $id=I('get.aid/d',0);
            $info =M('draft_article')->where(array('id'=>$id))->field('title,content')->select();
            foreach ($info as $val) {
                $data['title'] = $val['title'];
                $data['content'] = html_entity_decode($val['content']);
            }
            $this->assign('data',$data);
        }



        $this->assign('category',$category);

        $this->display('Contribute/index');
    }

    public function dopost()
    {

        // 上传图片
        if (!empty($_FILES)) {
                $upload = new \Think\Upload();
                $upload->rootPath = './Uploads/'; // 设置附件上传根目录
                $upload->savePath = 'cover/'; // 设置附件上传（子）目录
                $info = $upload -> uploadOne($_FILES['cover']);
                if(!$info) {
                    $this->error($upload->getError());//获得上传附件产生的错误信息
                } else {
                       $cover =  $info['savepath'].$info['savename'];
                        $_POST['cover'] = $cover;
                        $_POST['img'] = $cover;
            }

        }

        // 判断文件是否上传
        if ($_POST['content'] == '' || $_POST['title'] == '') {
            $this->redirect('Contribute/index');
            exit;
        }
       
        $_POST['content'] =htmlentities($_POST['content']);
        $_POST['title'] = $_POST['title'];
        $_POST['user_id'] = $_SESSION['user']['id'];
        $_POST['create_time'] = date('Y-m-d H:i:s');
        $_POST['time'] =time();


        // V($_POST);
        M('article')->create();
        if ($art_id=M('article')->add()) {
                // V($cate_id);
                // $POST['cat_id'] =$cate_id;
                // 将封面图片上传到img图片库
               M('image')->add(array('art_id'=>$art_id,'img'=>$_POST['img']));

            $this->success('您已成功发布!',U('Myarticle/article'));
        } else {

            $this->error('发布失败!');
        }
    }
}